package com.glxy.controller;

import com.glxy.dao.impl.ProviderDaoImpl;
import com.glxy.pojo.Bill;
import com.glxy.pojo.Provider;
import com.glxy.pojo.User;
import com.glxy.service.impl.BillServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;

public class BillServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        String action = request.getParameter("action");
        BillServiceImpl billService = new BillServiceImpl();
        request.setCharacterEncoding("utf-8");

        if(action.equals("list")){
            int pageNo = 1;
            String pageNos = request.getParameter("pageNo");
            if(pageNos!=null){
                pageNo = Integer.parseInt(pageNos);
            }
            int pageSize = 5;

            int totalCouunt = billService.totalCount();
            int totalpage = 0;

            if(totalCouunt%pageSize==0){
                totalpage = totalCouunt/pageSize;
            }else {
                totalpage = totalCouunt/pageSize+1;
            }


            int prev = pageNo;
            int next = pageNo;
            if(pageNo > 1){
                prev--;
            }
            if (pageNo < totalpage){
                next++;
            }

            ArrayList<Bill> list = billService.findAllBillPages(pageSize,pageNo);
            ArrayList<Provider> providers = new ProviderDaoImpl().findAll();

            request.setAttribute("totalPage",totalpage);
            request.setAttribute("prev",prev);
            request.setAttribute("next",next);
            request.setAttribute("pageNo",pageNo);
            request.setAttribute("list",list);
            request.setAttribute("providers", providers);
            request.getRequestDispatcher("billList.jsp").forward(request,response);

        }else if (action.equals("search")) {
            String productName = request.getParameter("productName");

            ArrayList<Bill> list = billService.findBill(productName);

            for (Bill bill : list) {
                System.out.println(bill.getBillCode());
                System.out.println(bill.getId());
                System.out.println(bill.getProductName());
            }

            request.setAttribute("list", list);
            request.getRequestDispatcher("billList.jsp").forward(request, response);

        }else if (action.equals("add")){
            Bill bill = new Bill();
            bill.setId(Long.valueOf(request.getParameter("billId")));
            bill.setProductName(request.getParameter("billName"));
            bill.setProductUnit(request.getParameter("billCom"));
            bill.setProductCount(BigDecimal.valueOf(Long.parseLong(request.getParameter("billNum"))));
            bill.setTotalPrice(BigDecimal.valueOf(Double.parseDouble(request.getParameter("money"))));

            billService.insertBill(bill);

            request.getRequestDispatcher("BillServlet?action=list").forward(request,response);
        }else if(action.equals("view")) {

            Bill bill = billService.findBill(request.getParameter("name")).get(0);

            request.setAttribute("bill",bill);

            request.getRequestDispatcher("billView.jsp").forward(request,response);

        }else if (action.equals("update")){
            String name = request.getParameter("name");

            Bill bill = billService.findBill(name).get(0);

            request.setAttribute("bill", bill);
            request.getRequestDispatcher("billUpdate.jsp").forward(request, response);

        }else if (action.equals("updateSave")){
            Bill bill = billService.findBill(request.getParameter("oriName")).get(0);

            bill.setId(Long.valueOf(request.getParameter("billId")));
            bill.setProductName(request.getParameter("productName"));
            bill.setProductUnit(request.getParameter("productUnit"));
            bill.setProductCount(BigDecimal.valueOf(Long.parseLong(request.getParameter("productCount"))));
            bill.setTotalPrice(BigDecimal.valueOf(Double.parseDouble(request.getParameter("totalPrice"))));
            bill.setProviderId(Long.valueOf(request.getParameter("providerId")));

            billService.update(bill);

            request.getRequestDispatcher("UserServlet?action=list").forward(request,response);

        } else if (action.equals("delete")){
            String name = request.getParameter("name");

            Bill bill = billService.findBill(name).get(0);

            billService.delete(bill.getId());

            request.getRequestDispatcher("BillServlet?action=list").forward(request, response);
        }

    }
}
